import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class Demo {

    @Test
    public void demo(){
        //创建连接
        MongoClient mongoClient = new MongoClient("10.31.162.13");
        //打开数据库
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //获取文档
        FindIterable<Document> documents = spit.find();
        for (Document document : documents) {
            System.out.println(document);
        }
        mongoClient.close();
    }



    @Test
    public void demo1(){
        //创建连接
        MongoClient mongoClient = new MongoClient("10.31.162.13");
        //打开数据库
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
       //构建查询条件
        BasicDBObject bson = new BasicDBObject("userid", "1013");
        //获取文档集合结果
        FindIterable<Document> documents = spit.find(bson);
        for (Document document : documents) {
            System.out.println(document);
        }
        mongoClient.close();
    }

        //--------------------------条件查询
    @Test
    public void demo2(){
        //创建连接
        MongoClient mongoClient = new MongoClient("10.31.162.13");
        //打开数据库
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //构建查询条件
        BasicDBObject bson = new BasicDBObject("visits", new BasicDBObject("$gt",1000));
        //获取文档集合结果
        FindIterable<Document> documents = spit.find(bson);
        for (Document document : documents) {
            System.out.println(document);
        }
        mongoClient.close();
    }


    @Test
    public void demo3(){
        //创建连接
        MongoClient mongoClient = new MongoClient("10.31.162.13");
        //打开数据库
        MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");

        Map<String,Object> map=new HashMap();

        mongoClient.close();
    }
}
